import Item from '~/utils/ali/Item'
import {TRequest} from '~/utils/ali/PCRequest'

export class TaoItem implements ItemInterface {
    private itemData: any = {}
    private info: any = {}
    private exParams: any = {}
    private mobileInfo: any = {}

    constructor() {
        // this.exParams = this.getQueryParams(document.querySelector('#tb-beacon-aplus').attributes.getNamedItem('exparams').value)
        this.getDetail()
    }

    itemSimilar(itemId: string): Promise<any> {
        return Item.itemSimilar(itemId)
    }

    auctionImages() {
        return new Promise((resolve) => {
            resolve(this.info.item.images || [])
        })
    }

    auctionVideos() {
        return new Promise((resolve) => {
            resolve(this.info.item.videos || [])
        })
    }

    skuImages() {
        return new Promise((resolve) => {
            
            console.log("🚀 ~ TaoItem ~ returnnewPromise ~ this.info:", this.info)
            const images: any = []
            if (this.info.skuBase.props) {
                this.info.skuBase.props.forEach(item => {
                    item.values.forEach(it => {
                        images.push(it)
                    })
                });
            }
            resolve(images)
        })
    }

    mobileDetail(): Promise<any> {
        return Item.mobileDetail(this.itemId())
    }

    mobileAuctionImages() {
        return Item.mobileAuctionImages(this.itemId())
    }

    mobileAuctionVideos() {
        return Item.mobileAuctionVideos(this.itemId())
    }

    mobileDescImages() {
        return Item.mobileDescImages(this.itemId())
    }

    utcItemShowList(offset: number | string, pageSize: number | string) {
        return Item.utcItemShowList(this.itemId(), offset, pageSize, true)
    }

    descImages() {
        return new Promise((resolve) => {
            TRequest.pcTmDesc().then((res: any) => {
                const images: Array<string> = []
                const descHtml = res.data.pcDescContent
                descHtml.replace(/<img[^>]*>/g, (match: any) => {
                    const src = match.match(/src="([^"]*)"/)[1]
                    images.push(src)
                    return ''
                })
                resolve(images)
            })
        })
    }

    cid() {
        if (typeof this.itemData.cid === 'undefined' && Object.keys(this.info).length > 0) {
            this.itemData.cid = this.info?.item?.categoryId
        }
        return this.itemData.cid || -1;
    }

    // 评论接口 https://h5api.m.taobao.com/h5/mtop.taobao.rate.detaillist.get/5.0/?jsv=2.7.0&appKey=12574478&t=1685531793280&api=mtop.taobao.rate.detaillist.get&v=5.0&ecode=1&type=jsonp&timeout=20000&dataType=jsonp&sessionOption=AutoLoginOnly&jsonpIncPrefix=haloMtop&data=%7B%22showTrueCount%22%3Afalse%2C%22auctionNumId%22%3A%22627135490680%22%2C%22pageNo%22%3A6%2C%22pageSize%22%3A20%2C%22expression%22%3A%22%22%2C%22rateType%22%3A%22%22%2C%22searchImpr%22%3A%22-8%22%2C%22orderType%22%3A%22%22%7D&sign=c54261d1e49bfd450949246ef9dc7e6d&callback=callbackc54261d1e49bfd450949246ef9dc7e6d&callback=callbackc54261d1e49bfd450949246ef9dc7e6d&_=1685531781194
    itemId() {
        if (typeof this.itemData.itemId === 'undefined') {
            const match = location.href.match(/[&|?]id=(\d+)/)
            if (match) {
                this.itemData.itemId = match[1]
            }

            return this.itemData.itemId
        } else {
            return this.itemData.itemId
        }
    }

    sellerId() {
        if (typeof this.itemData.sellerId === 'undefined') {
            this.itemData.sellerId = this.info.seller.sellerId
        }

        return this.itemData.sellerId
    }

    sellerNick() {
        if (typeof this.itemData.sellerNick === 'undefined') {
            this.itemData.sellerNick = this.info.seller.sellerNick
        }

        return this.itemData.sellerNick
    }

    shopId() {
        if (typeof this.itemData.shopId === 'undefined') {
            this.itemData.shopId = this.info.seller.shopId
        }

        return this.itemData.shopId
    }

    shopName() {
        // console.log(this.info.seller)
        if (typeof this.itemData.shopName === 'undefined') {
            this.itemData.shopName = this.info.seller.shopName
        }

        return this.itemData.shopName
    }

    getDetail(): Promise<any> {
        return new Promise((resolve, reject) => {
            if (Object.keys(this.info).length > 0) {
                resolve(this.info)
            }
            TRequest.mobileDetail(this.itemId()).then((res) => {
                this.info = res
                resolve(res)
            }).catch(() => {
                setTimeout(() => {
                    this.getDetail().then((res) => {
                        resolve(res)
                    })
                }, 5000);
            })
        })
    }

    getQueryParams(url: string) {
        const params: any = {}
        const regex = /&*([^=#]+)=([^&#]*)/g
        let match
        // eslint-disable-next-line no-cond-assign
        while (match = regex.exec(url)) {
            params[match[1]] = decodeURIComponent(match[2].replace(/\+/g, ' '))
        }

        return params
    }

    shopUrl() {
        return `https://shop${this.shopId()}.taobao.com`
    }

    itemName() {
        if (typeof this.itemData.itemName === 'undefined') {
            this.itemData.itemName = document.querySelector('h1[class^=ItemHeader--mainTitle]')?.innerHTML;
        }

        return this.itemData.itemName
    }

}
